<?php
/**
 * Created by PhpStorm.
 * User: liyu
 * Date: 2018/4/9
 * Time: 下午4:54
 */

namespace app\api\model;


use app\common\service\Password;

class User extends Base
{
    protected $autoWriteTimestamp = 'datetime';

    protected $readonly = ['guid'];

    protected $insert = ['guid','password'=>'123456'];

    public function org()
    {
        return $this->hasOne('Org','id','org_id')->field(['id','name']);
    }

    public function setGuidAttr()
    {
        while(true){
            $gid = $this->create_gid();
            if ($this->where(['guid'=>$gid])->count()>0)
                continue;
            return $gid;
        }
    }

    public function getGenderAttr($value)
    {
        switch($value){
            case 0:
                $data = '保密';break;
            case 1:
                $data = '男';break;
            case 2:
                $data = '女';break;
            default:
                $data = '未知错误';break;
        }

        return $data;
    }

    public function getEnabledAttr($value)
    {
        switch($value){
            case 0:
                $data = '正常';break;
            case 1:
                $data = '禁用';break;
            default:
                $data = '未知错误';break;
        }

        return $data;
    }

    public function getBirthdayAttr($value)
    {
        if(empty($value))
            return '';
    }

    public function setPasswordAttr($value)
    {
        return Password::hash($value);
    }

    protected static function init()
    {
//        self::event('after_insert',function($user){
//
//        });
    }
}